﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Protogamez_Installer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// SET GLOBAL FUNCTION
        /// </summary>
        private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
        {
            DirectoryInfo dir = new DirectoryInfo(sourceDirName);
            DirectoryInfo[] dirs = dir.GetDirectories();

            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                if (System.IO.Path.Combine(file.Name) != "Thumbs.db")
                {
                    if (System.IO.Path.Combine(file.Name) != "gamesdb.mdb")
                    {
                        string temppath = System.IO.Path.Combine(destDirName, file.Name);
                        file.CopyTo(temppath, true);
                    }
                }
            }

            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = System.IO.Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }
            }
        }

        /// <summary>
        /// GLOBAL SOURCE CODE
        /// </summary>
        private void windows_main_Loaded(object sender, RoutedEventArgs e)
        {
            string versionI = System.IO.File.ReadAllText(Properties.Settings.Default.install + @"\version.int");
            string versionU = System.IO.File.ReadAllText(Properties.Settings.Default.update + @"\version.int");

            if (versionI != versionU)
            {
                label_update.Visibility = Visibility.Visible;
            }
            else
            {
                label_update.Visibility = Visibility.Hidden;
            }

            label_RV.Content = versionU;

            textbox_install.Text = Properties.Settings.Default.install;
            textbox_update.Text = Properties.Settings.Default.update;

            string db = Properties.Settings.Default.db;
            string ressource = Properties.Settings.Default.ressource;

            if (db == "1")
            {
                checkbox_db.IsChecked = checked(true);
            }

            if (ressource == "1")
            {
                checkbox_ressource.IsChecked = checked(true);
            }
        }

        private void button_install_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            Properties.Settings.Default.install = dialog.SelectedPath;
            Properties.Settings.Default.Save();

            textbox_install.Text = Properties.Settings.Default.install;
        }

        private void button_update_Click(object sender, RoutedEventArgs e)
        {
            var dialog = new System.Windows.Forms.FolderBrowserDialog();
            System.Windows.Forms.DialogResult result = dialog.ShowDialog();

            Properties.Settings.Default.update = dialog.SelectedPath;
            Properties.Settings.Default.Save();

            textbox_update.Text = Properties.Settings.Default.update;
        }

        private void checkbox_db_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.db = "1";
            Properties.Settings.Default.Save();
        }

        private void checkbox_db_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.db = "0";
            Properties.Settings.Default.Save();
        }

        private void checkbox_ressource_Checked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ressource = "1";
            Properties.Settings.Default.Save();
        }

        private void checkbox_ressource_Unchecked(object sender, RoutedEventArgs e)
        {
            Properties.Settings.Default.ressource = "0";
            Properties.Settings.Default.Save();
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string install = Properties.Settings.Default.install;
            string update = Properties.Settings.Default.update;
            string db = Properties.Settings.Default.db;
            string ressource = Properties.Settings.Default.ressource;

            File.Copy(update + @"\Protogamez.application", install + @"\Protogamez.application", true);
            File.Copy(update + @"\Protogamez.exe", install + @"\Protogamez.exe", true);
            File.Copy(update + @"\Protogamez.exe.config", install + @"\Protogamez.exe.config", true);
            File.Copy(update + @"\Protogamez.exe.manifest", install + @"\Protogamez.exe.manifest", true);
            File.Copy(update + @"\version.int", install + @"\version.int", true);

            if (db == "1")
            {
                File.Copy(update + @"\Resources\gamesdb.mdb", install + @"\Resources\gamesdb.mdb", true);
            }

            if (ressource == "1")
            {
                DirectoryCopy(update + @"\Resources\", install + @"\Resources\", false);
            }

            MessageBox.Show("The install or update, was completed." + Environment.NewLine + Environment.NewLine + "From: " + update + Environment.NewLine + Environment.NewLine + "To: " + install + Environment.NewLine + Environment.NewLine + "With DB option: " + db + " and ressource option " + ressource);

            this.Close();
        }

        private void windows_main_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            var processStartInfo = new ProcessStartInfo(@"C:\Protogamez\Protogamez.exe");
            processStartInfo.WorkingDirectory = System.IO.Path.GetDirectoryName(@"C:\Protogamez\");
            Process gameProcess = Process.Start(processStartInfo);
        }
    }
}
